# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

#This uses CMake instead of meson because the CMake build
#generates *.cmake that are used by other libraries that
#depend upon CMake's findpackage function.

_realname=directx-headers
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.610.0
pkgrel=1
pkgdesc="Official DirectX headers available under an open source license (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://github.com/microsoft/DirectX-Headers'
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("$url/archive/refs/tags/v$pkgver.tar.gz"
       fix-cmake.patch
       DirectX-Headers.pc.in)
sha256sums=('343b04a8206c4169a1feab99a7bd29ecb0c8f511988e9a007fea51768bda14fa'
            '899a018a4107855a74cc6cee783341c2347be22a5b704cc26ccca8c5f182d9ed'
            'ee973cd0715cc2264543e404d2a6bf885be823a34e4eb371a27901d7dae01c08')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

# fix RPCNDR version incompatibility
  patch -Np1 -i "${srcdir}"/fix-cmake.patch
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DDXHEADERS_BUILD_TEST=OFF -DDXHEADERS_BUILD_GOOGLE_TEST=OFF \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
  
# Generate DirectX-Headers.pc manually for software that doesn't use
# CMake for builds such as mesa.
  sed ${srcdir}/DirectX-Headers.pc.in \
    -e "s|VERSION|$pkgver|g" \
    -e "s|MINGW_PREFIX|${MINGW_PREFIX}|g" > DirectX-Headers.pc
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "DirectX-Headers.pc" "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/DirectX-Headers.pc"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
